package com.mcx.visplat.controller;

import com.mcx.visplat.mapper.StationMapper;
import com.mcx.visplat.pojo.Pollutant;
import com.mcx.visplat.pojo.Station;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping
public class StationController {

    @Resource
    StationMapper stationMapper;

    /**
     *  获取某个站点某时间节点的前24小时的数据
     * @param stationId
     * @param time
     * @return
     */
    @GetMapping("/getLastStationInfo")
    public List<Station> getLastStationInfo(@RequestParam("stationId") int stationId,@RequestParam("time") String time){
        return stationMapper.getLastStationInfo(stationId, time);
    }

    /**
     *  获取某个站点某个时间段的数据
     * @param stationId
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getTimeslotStationInfo")
    public List<Station> getTimeslotStationInfo(@RequestParam("stationId") int stationId,
                                                @RequestParam("startTime") String startTime,
                                                @RequestParam("endTime") String endTime){
        return stationMapper.getTimeslotStationInfo(stationId, startTime, endTime);
    }

    /**
     * 获取某个站点某个时间前6个小时的超标数据
     * @param stationId
     * @param time
     * @return
     */
    @GetMapping("/getPollutant")
    public List<Pollutant> getPollutant(@RequestParam("stationId") int stationId, @RequestParam("time") String time){
        List<Station> stationList = stationMapper.getLastStationInfo(stationId, time);
        List<Pollutant> pollutantList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.0");
        for(Station s : stationList){
            Pollutant p;
            if(s.getPM25() > 85){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("PM2.5");
                p.setValue(s.getPM25());
                p.setExValue(Double.parseDouble(df.format(s.getPM25() - 85)));
                pollutantList.add(p);
            }
            if(s.getPM10() > 150){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("PM10");
                p.setValue(s.getPM10());
                p.setExValue(Double.parseDouble(df.format(s.getPM10() - 150)));
                pollutantList.add(p);
            }
            if(s.getCO() > 1.2){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("CO");
                p.setValue(s.getCO());
                p.setExValue(Double.parseDouble(df.format(s.getCO() - 1.2)));
                pollutantList.add(p);
            }
            if(s.getNO2() > 100){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("NO2");
                p.setValue(s.getNO2());
                p.setExValue(Double.parseDouble(df.format(s.getNO2() - 100)));
                pollutantList.add(p);
            }
            if(s.getSO2() > 15){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("SO2");
                p.setValue(s.getSO2());
                p.setExValue(Double.parseDouble(df.format(s.getSO2() - 15)));
                pollutantList.add(p);
            }
            if(s.getO3() > 160){
                p = new Pollutant();
                p.setTime(s.getTime());
                p.setPollutantName("O3");
                p.setValue(s.getO3());
                p.setExValue(Double.parseDouble(df.format(s.getO3() - 160)));
                pollutantList.add(p);
            }
        }
        return pollutantList;
    }


    /**
     *获取几个站点某个时间段的数据
     * @param stationId
     * @param time
     * @return
     */
    @GetMapping("/getStationsInfo")
    public List<Station> getStationsInfo(@RequestParam("stationId[]") Integer[] stationId,
                                         @RequestParam("time") String time){
        return stationMapper.getStationsInfo(stationId, time);
    }
}
